#-*- coding:utf-8 -*-

import sys
from common.config import *
import json
import time
from common.opmysql import *
import websocket
import allure
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

class Testpx():
    @allure.step('根据appid获取逛店活动基本信息')
    def getcloudwalk(self,env, appid):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/cloudwalk/detail/get"
        data = {"appid": appid}
        r = requests.post(url, json=data, headers=headers)
        return r

    @allure.step('店铺列表')
    def shoplist(self,env,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/cloudwalk/shop/list"
        data = {"cloudwalkId":cloudwalkId,"playerId":playerId}
        r = requests.post(url, json=data, headers=headers)
        return r

    @allure.step('小程序获取openId')
    def getOpenId(self,env,appid,jsCode):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/applet/getOpenId"
        data = {"appid":appid,"jsCode":jsCode}
        r = requests.post(url, json=data, headers=headers)
        return r

    @allure.step('店铺详情')
    def detailget(self,env,id,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/cloudwalk/shop/detail/get"
        data = {"playerId":playerId,"id":id,"cloudwalkId":cloudwalkId}
        r = requests.post(url, json=data, headers=headers)
        return r

    @allure.step('后台获取店铺信息')
    def shopget(self,env,id):
        zhongtai_url = getconf(env, "zhongtai_url")
        token = buslogin(env)
        headers = {"Content-Type": "application/json","Token": token}
        url = zhongtai_url + "cloudwalk-manager/v1/shop/"+str(id)
        print (url)
        r = requests.get(url, headers=headers)
        return r

    @allure.step('店铺交互领券接口')
    def couponget(self,env,cloudwalkId,playerId,shopId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/cloudwalk/coupon/get"
        data = {"cloudwalkId":cloudwalkId,"playerId":playerId,"shopId":shopId}
        r = requests.post(url, json=data, headers=headers)
        return r

    @allure.step('ICON配置')
    def iconget(self,env,cloudwalkId,parentId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/cloudwalk/icon/get"
        data = {"cloudwalkId":cloudwalkId,"parentId":parentId}
        r = requests.post(url, json=data, headers=headers)
        return r

    @allure.step('获取玩家信息')
    def getplayer(self,env, openId,cloudwalkId,appid):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/baseInfo/get"
        data = {"openId": openId,"cloudwalkId":cloudwalkId,"appid":appid}
        r = requests.post(url, json=data, headers=headers)
        return r

    @allure.step('初始化玩家信息')
    def initplayer(self,env, openId,cloudwalkId,gender,appid):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/baseInfo/init"
        data = {"openId": openId,"cloudwalkId": cloudwalkId,"gender": gender,"appid":appid}
        # print (data)
        r = requests.post(url, json=data, headers=headers)
        return r

    @allure.step('用户注册')
    def registerplayer(self,env, openId,cloudwalkId,appid,playerId,phone,headPortrait,nickname):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/member/register"
        data = {"openId": openId,"cloudwalkId": cloudwalkId,"appid":appid,"playerId": playerId,"phone":phone,"headPortrait":headPortrait,"nickname":nickname}
        print (data)
        r = requests.post(url, json=data, headers=headers)
        return r

    @allure.step('用户签到列表')
    def signInlist(self,env,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/signIn/list"
        data = {"cloudwalkId": cloudwalkId,"playerId": playerId}
        r = requests.post(url, json=data, headers=headers)
        return r

    @allure.step('签到')
    def signIn(self,env,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/signIn/sign"
        data = {"cloudwalkId": cloudwalkId,"playerId": playerId}
        print (data)
        r = requests.post(url, json=data, headers=headers)
        return r

    @allure.step('加好友')
    def friendsadd(self,env,cloudwalkId,playerId,relationPlayerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/friends/add"
        data = {"cloudwalkId": cloudwalkId,"playerId": playerId,"relationPlayerId":relationPlayerId}
        r = requests.post(url, json=data, headers=headers)
        return r

    @allure.step('好友列表')
    def friendslist(self,env,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/friends/list"
        data = {"cloudwalkId": cloudwalkId,"playerId": playerId}
        r = requests.post(url, json=data, headers=headers)
        return r

    @allure.step('根据手机号获取玩家资料')
    def phonegetplayer(self,env, cloudwalkId,playerId,phone,appid):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/memberInfo/get"
        data = {"cloudwalkId": cloudwalkId,"playerId": playerId,"phone":phone,"appid":appid}
        print (data)
        r = requests.post(url, json=data, headers=headers)
        return r

    @allure.step('完善玩家资料')
    def updateplayer(self,env, cloudwalkId,playerId,id,appid,cardCode,realname,birthday):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/memberInfo/update"
        data = {"cloudwalkId": cloudwalkId,"playerId": playerId,"id":id,"appid":appid,"cardCode":cardCode,"realname":realname,"birthday":birthday}
        # print (data)
        r = requests.post(url, json=data, headers=headers)
        return r

    @allure.step('用户行为上报')
    #参数：云逛店id，玩家id，行为、行为对应值
    def taskreport(self,env,cloudwalkId,playerId,behavior,value,shopId):
        dayu_url = getconf(env,"dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/task/report"
        #behavior：1:登陆,2:行走步数,3:逛店,4:看道具商城,5:分享,6:开传送门,7:开商城服务面板,8:查看我的信息,9:查看自主积分,10:消耗金币,11:购买道具,12:世界频道发言,13:宠物喂食,14:爱抚宠物'
        #OPEN_PORTAL, PET_FEEDING, CONSUME_GOLD_COINS, LOGIN, WORLD_CHANNEL_SPEECH, STEP, SEE_PROPS_MALL, PET_CARESS, LOOK_SHOP, BUY_GOODS, OPEN_MALL_SERVICE, LOOK_MY_INFO, LOOK_INTEGRAL, SHARE
        if shopId==0:
            data = {"cloudwalkId": cloudwalkId,"playerId": playerId,"behavior": behavior,"value": value}
        else:
            data = {"cloudwalkId": cloudwalkId,"playerId": playerId,"behavior": behavior,"value": value,"shopId":shopId}
        # print(data)
        r = requests.post(url,json = data,headers = headers)
        return r

    @allure.step('获取玩家日常任务列表')
    #参数：云逛店id，玩家id
    def dailyTasklist(self,env,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/task/daily/get"
        data = {"cloudwalkId": cloudwalkId,"playerId": playerId}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('获取活跃度任务')
    #参数：云逛店id，玩家id
    def activationTask(self,env,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/task/activation/get"
        data = {"cloudwalkId": cloudwalkId,"playerId": playerId}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('获取主线任务')
    def threadtask(self,env,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/task/thread/get"
        data = {"cloudwalkId": cloudwalkId,"playerId": playerId}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('完成任务领奖')
    #参数：云逛店id，玩家id，任务id
    def receiveAwards(self,env,cloudwalkId,playerId,id):
        dayu_url = getconf(env, "dayu_url")
        headers = {"content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/task/award/receive"
        data = {"cloudwalkId": cloudwalkId,"playerId": playerId,"id": id}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('任务查看')
    def taskview(self,env,cloudwalkId,playerId,playerTaskId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/task/view"
        data = {"cloudwalkId": cloudwalkId,"playerId": playerId,"playerTaskId": playerTaskId}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('玩家任务new标识')
    def tasknewflag(self,env,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/task/list/new/flag"
        data = {"cloudwalkId": cloudwalkId,"playerId": playerId}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('未完成的任务列表')
    def tasklist(self,env,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/task/list"
        data = {"cloudwalkId": cloudwalkId,"playerId": playerId}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('道具商城商品列表')
    def goodslist(self,env,cloudwalkId,type):
        dayu_url = getconf(env, "dayu_url")
        headers = {"content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/cloudwalk/goods/list"
        data = {"cloudwalkId": cloudwalkId,"type":type}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('商品兑换')
    # COIN(1, “金币”), POINT(2, “积分”), COIN_AND_POINT(3, “金币 + 积分”), DIAMONDS(4,”钻石”), DIAMONDS_AND_POINT(5,”钻石 + 积分”)
    def exchange(self,env,cloudwalkId,playerId,goodsId,paymentType):
        dayu_url = getconf(env, "dayu_url")
        headers = {"content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/cloudwalk/goods/exchange"
        data = {"cloudwalkId": cloudwalkId,"playerId": playerId,"goodsId": goodsId,"paymentType":paymentType}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('根据id查询任务')
    def getTaskById(self,env,taskid):
        zhongtai_url = getconf(env, "zhongtai_url")
        token = buslogin(env)
        headers = {"Content-Type": "application/json", "Token": token}
        url = zhongtai_url + "cloudwalk-manager/v1/task/getTaskById"
        data = {"id": taskid}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('登录提示')
    def logintips(self,env,cloudwalkId,openId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/login/tips"
        data = {"cloudwalkId": cloudwalkId,"openId":openId}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('连续签到奖励列表')
    def continuitylist(self,env,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/signIn/continuity/list"
        data = {"cloudwalkId": cloudwalkId,"playerId":playerId}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('连续签到领奖')
    def awardreceive(self,env,cloudwalkId,playerId,day):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/signIn/continuity/award/receive"
        data = {"cloudwalkId": cloudwalkId,"playerId":playerId,"day":day}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('是否显示新手指引')
    def noviceguide(self,env,cloudwalkId,openId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/novice/guide"
        data = {"cloudwalkId": cloudwalkId,"openId":openId}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('查询玩家宠物')
    def queryplayerpet(self,env,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/pet/queryplayerpet"
        data = {"cloudwalkId": cloudwalkId,"playerId":playerId}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('查询玩家宠物道具')
    def queryplayerprop(self,env,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/prop/queryplayerprop"
        data = {"cloudwalkId": cloudwalkId,"playerId":playerId}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('玩家使用中道具')
    def queryuseprop(self,env,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/prop/queryuseprop"
        data = {"cloudwalkId": cloudwalkId,"playerId":playerId}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('宠物图鉴')
    def petlist(self,env,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/pet/petlist"
        data = {"cloudwalkId": cloudwalkId,"playerId":playerId}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('玩家宠物列表')
    def petqueryList(self,env,cloudwalkId,playerId):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/pet/queryList"
        data = {"cloudwalkId": cloudwalkId,"playerId":playerId}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('玩家宠物放出/收回')
    # 养成状态：0：收回1：放出
    def petupstatus(self,env,cloudwalkId,playerId,id,upStatus):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/pet/change/upstatus"
        data = {"cloudwalkId": cloudwalkId,"playerId":playerId,"id":id,"upStatus":upStatus}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('玩家宠物治疗')
    def pettreatment(self,env,cloudwalkId,playerId,id,upStatus):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/pet/treatment"
        data = {"cloudwalkId": cloudwalkId,"playerId":playerId,"id":id,"upStatus":upStatus}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('免费领取宠物道具')
    # 道具类型    1：饲料2：撸宠
    def propfreeprop(self,env,cloudwalkId,playerId,propType):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/prop/freeprop"
        data = {"cloudwalkId": cloudwalkId,"playerId":playerId,"propType":propType}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('玩家使用道具')
    def playerpropuseprop(self,env,cloudwalkId,playerId,propType,id):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/prop/useprop"
        data = {"cloudwalkId": cloudwalkId,"playerId":playerId,"propType":propType,"id":id}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('领取宠物等级奖励')
    def levelawardreceive(self,env,cloudwalkId,playerId,id):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/pet/level/award/receive"
        data = {"cloudwalkId": cloudwalkId,"playerId":playerId,"id":id}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('修改宠物名称')
    def petnameset(self,env,cloudwalkId,playerId,spaceName,id,nickname):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "cloudwalk-portal/v1/player/pet/name/set"
        if id==0:
            data = {"cloudwalkId": cloudwalkId, "playerId": playerId, "spaceName": spaceName}
        else:
            data = {"cloudwalkId": cloudwalkId,"playerId":playerId,"id":id,"nickname":nickname,"spaceName":spaceName}
        r = requests.post(url,json=data,headers=headers)
        return r

    @allure.step('基础配置更新')
    def cloudwalkupdate(self,env,isLoginTips,cloudwalkId,frequency,interactType):
        dayu_url = getconf(env, "dayu_url")
        appid=getconf(env,"appid")
        token = buslogin(env)
        headers = {"Content-Type": "application/json", "Token": token}
        url = dayu_url + "cloudwalk-manager/v1/cloudwalk/update"
        print(1112)
        if isLoginTips=='NO':
            data = {"isLoginTips":isLoginTips,"id":cloudwalkId,"appid":appid}
        elif interactType=='POPUP_WINDOW':
            data = {"isLoginTips": isLoginTips, "id": cloudwalkId, "appid": appid,"loginTipsSpeech":"欢迎登陆福利岛",
                    "loginTipsPicture": "https://buorgcos.rongyi.com/cloudWalk/pic/67989767ba0f4360b5bfa85968de5bbc.png",
                    "frequency":frequency,"interactType":interactType,"loginTipsSpeech": "233232"}
        elif interactType=='GET_COUPON':
            if env=="test":
                data = {"isLoginTips": isLoginTips, "id": cloudwalkId, "appid": appid,"couponName": "自动化测试券","couponId": 1714,
                        "loginTipsPicture": "https://buorgcos.rongyi.com/cloudWalk/pic/67989767ba0f4360b5bfa85968de5bbc.png",
                        "frequency":frequency,"interactType":interactType}
            elif env=="dockertest":
                data = {"isLoginTips": isLoginTips, "id": cloudwalkId, "appid": appid,"couponName": "游戏专用测试券","couponId": 606,
                        "loginTipsPicture": "https://buorgcos.rongyi.com/cloudWalk/pic/67989767ba0f4360b5bfa85968de5bbc.png",
                        "frequency":frequency,"interactType":interactType}
            else:
                data = {"isLoginTips": isLoginTips, "id": cloudwalkId, "appid": appid,"couponName": "口红机压测券2","couponId": 150,
                        "loginTipsPicture": "https://buorgcos.rongyi.com/cloudWalk/pic/67989767ba0f4360b5bfa85968de5bbc.png",
                        "frequency":frequency,"interactType":interactType}
        print (data)
        r = requests.post(url,json=data,headers=headers)
        return r

if __name__ == "__main__":
    # r = Testpx().getcloudwalk("test","wxe5315e55aeda9e62")
    # r=Testpx().goodslist("test",4,"PET")
    # r=Testpx().receiveAwards("test",4,577,108851)
    # r=Testpx().shoplist("test",4)
    # r=Testpx().test_getgoodslist("test",4,'TOOL')
    # r=Testpx().friendslist("test",4,40676)
    # r=Testpx().detailget("test",3,4,40676)
    # r=Testpx().iconget("test",4,0)
    # r=Testpx().getOpenId("test","wxc8384b5ffde351b0","001bbuTm1CrJMo0RDAVm1lwtTm1bbuTM")
    # r=Testpx().getplayer("test","t8HNZA74Jo5n6wGjuFeXf1YmcdaD",4,"wxc8384b5ffde351b0")
    # r=Testpx().getplayer("test", "U5_Enu7sobc3mSqGvRZrY40CtlXI", 4, "wxc8384b5ffde351b0")
    # r=Testpx().signInlist("test", 4, 471)
    # r=Testpx().noviceguide("test", 4,"oOF_F5PLTu3rCZr-UDQSy6jfQyus" )
    # r=Testpx().friendsadd("test",4,"41075","41079")
    # r=Testpx().cloudwalkupdate("dockertest","YES",4,"EVERY_LOGIN","POPUP_WINDOW")
    r=Testpx().dailyTasklist("test",4,58208)
    # r=Testpx().receiveAwards("dockertest",5,46756,1621)
    # r=Testpx().exchange("dockertest", 5, 46756,27,"POINT")
    # r = Testpx().queryplayerpet("test",4,554)
    # r = Testpx().queryuseprop("test",4,556)
    # r = Testpx().test_fullprizeget("test",4,554,24)
    # r = Testpx(). queryplayerprop("test",4,554)
    # r = Testpx(). logintips("test",554,"oOF_F5PLTu3rCZr-UDQSy6jfQyus")
    # r = Testpx().petqueryList("test",4,554)
    print (r.json())

